/**
 *@Description 订单详情页
 *@Author zhangnf
 *@Date 2015-09-25
 */
var orderViewCtrl  = new XFApp.client();
XFApp.client.prototype.orderView = {
	successCode : "10000",
	page : null,
	pageGoods : null,
	pageCus : null,
	goodsSearchView : null,
	cusSearchView : null,
	urlItems : {
		getOrderByIdURL : basePath+"/userCenter/order/getOrderById",
		getUpDownOrderIdURL : basePath+"/userCenter/order/getUpDownOrderId",
		payOrderURL	: basePath+"/userCenter/order/payOrder"
	},
	initData : function(orderId){
		_this = this;
		$.ajax( {  
		    url:_this.urlItems.getOrderByIdURL,
		    data:{id : orderId},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(data.code !=_this.successCode
		    			|| null == data.result){
	        		return;
	        	}
		    	_this.renderDataToView(data.result);
		     },  
		     error : function() {  
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	renderDataToView : function(order){
		$("#orderNo").text(order.orderNo);
		$("#orderStatusStr").text(dksCtrl.dockerstatus.get(order.orderStatus));
		$("#orderStatus").val(order.orderStatus);
		$("#saleOrderNo").text(order.saleOrderNo);
    	$("#trackingNumber").text(order.trackingNumber);
    	$("#freight").text(order.freight);
    	$("#goodsValue").text(order.goodsValue);
    	$("#consignee").text(order.consignee);
    	$("#consigneeTel").text(order.consigneeTel);
    	$("#consigneePost").text(order.consigneePost);
    	$("#consigneeAdd").text(order.consigneeAdd);
    	
    	if(1 == order.payStatus){
    		$("#payBtn").hide();
    	}
    	
    	if(1==order.payType){
    		$("#payType0").removeAttr("checked");
    		$("#payType1").prop("checked","true");
    	}
    	
    	$("#currencyHidden").val(order.currency);
    	$("#nationHidden").val(order.consigneeNation);
    	    	
    	//商品详情
    	var details = order.orderDetailLst;
    	for(i=0 ;i<details.length; i++){
    		var _html = '';
    		_html += ''
    			+'	<tr>'
    			+'		<td align="center">'+details[i].sku+'</td> '
    			+'		<td align="center">'+this.getStr(details[i].itemName)+'</td> '
    			+'	 	<td align="center">'+details[i].price+'</td> '
    			+'		<td align="center">'+details[i].qty+this.getStr(details[i].unit)+'</td> '
    			+'		<td align="center">'+this.getStr(details[i].brand)+'</td> '
    			+'		<td align="center">'+this.getStr(details[i].gModel)+'</td> '
    			+'		<td align="center">'+details[i].netWeight+'</td> '
    			+'		<td align="center">'+details[i].grossWeight+'</td> '
    			+'	</tr> ';
    		$("#orderDetailTable tbody").append(_html);
    	}
	},
	getStr : function(str){
		if(null == str
			|| "null" == str
				|| "NULL" == str){
			return "";
		}else{
			return str
		}
	},
	pay : function(){
		_this = this;
		webAlert({
            title:'提示',
            content:'确定支付订单:'+$("#orderNo").text()+'吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.doPayOrder();
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	doPayOrder : function(){
		_this = this;
		$.ajax({
		    url:_this.urlItems.payOrderURL,
		    data:{id:$("#orderId").val()},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		$("#payBtn").hide();
		    	}
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	edit : function(){
		var orderStatus = $("#orderStatus").val();
		if("29" == orderStatus || 30==orderStatus || "20" == orderStatus){
			window.location.href=basePath+"/userCenter/order/detail?id="+$("#orderId").val();
		}else{
			window.webDialogUtil.alert("提示","订单已提交,无法修改");
		}
	},
	//0上1下
	upDown : function(flag){
		var orderId = $("#orderId").val();
		$.ajax( {  
		    url:_this.urlItems.getUpDownOrderIdURL,
		    data:{id : orderId,flag:flag},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(data.code !=_this.successCode
		    			|| null == data.result){
		    		if(null == data.result && 0 == flag){
		    			window.webDialogUtil.alert("提示","没有上一个了");
		    		}else{
		    			window.webDialogUtil.alert("提示","没有下一个了");
		    		}
	        		return;
	        	}else{
	        		window.location.href=basePath+"/userCenter/order/view?id="+data.result;
	        	}
		     },
		     error : function() {  
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;
		
		$("#payBtn").on("click",function(){
			_this.pay();
		});
		$("#editBtn").on("click",function(){
			_this.edit();
		});
		$("#upBtn").on("click",function(){
			_this.upDown(0);//0上1下
		});
		$("#downBtn").on("click",function(){
			_this.upDown(1);//0上1下
		});
	},
};
$(function(){
	//初始化查看数据
	var orderId = $("#orderId").val();
	orderViewCtrl.orderView.initData(orderId);
	
	//初始化事件
	orderViewCtrl.orderView.event();
});